using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Core.Graphics;

namespace WorldEditor
{
    public partial class Pallet : UserControl
    {
        public TileSet TileSet { get { return _TileSet; } }
        TileSet _TileSet;
        public event EventHandler TileSelected;

        public Pallet()
        {
            _TileSet = null;
            InitializeComponent();
        }

        public Pallet(TileSet TileSet)
        {
            _TileSet = TileSet;
            InitializeComponent();
        }

        private void Pallet_Load(object sender, EventArgs e)
        {
            if (_TileSet != null)
            {
                //Change dimensions of this control based on number of tiles
                this.Width = (3 + Tile.TileWidth) * _TileSet.TilesPerRow;

                foreach (TileSetTile t in _TileSet.Tiles)
                {
                    //Create PalletTile
                    PalletTile pt = new PalletTile(t);
                    pt.PalletTileSelected += new EventHandler(pt_PalletTileSelected);
                    //Add PalletTile to FlowPanel
                    flpPalletTiles.Controls.Add(pt);
                }
            }
        }

        private void pt_PalletTileSelected(object sender, EventArgs e)
        {
            //Update the borders that signify which is the currently active tile
            foreach (PalletTile pt in flpPalletTiles.Controls)
            {
                if (pt == sender)
                {
                    pt.IsActiveTile = true;
                }
                else
                {
                    pt.IsActiveTile = false;
                }
            }

            if (TileSelected != null)
                TileSelected.Invoke(sender, e);
        }
    }
}
